
import axios from 'axios'
import store from '@/store'
import { getToken, getTokenType } from '@/utils/auth'

const service = axios.create({
  withCredentials: true,
  timeout: 15000
})

// 请求拦截
service.interceptors.request.use(
  config => {
    config.headers['Authorization'] = getTokenType() + ' ' + getToken()
    return config
  },
  error => {
    return Promise.reject(error)
  }
)

// 响应拦截
service.interceptors.response.use(
  response => {
    if (response.data.code !== '200') {
      if (response.data.code === '3002') {
        // 回退到登录页
      } else if (
        response.data.code === '3005' ||
        response.data.code === '3007'
      ) {
        // 登录过期 回退到登录页
      } else {
        // 错误提示
        // Notify({ type: 'danger', message: response.data.message })
        // return response
      }
      return response
      // return Promise.reject(new Error(response.data.message || 'Error'))
    } else {
      return response
    }
  },
  error => {
    // 错误提示
    return Promise.reject(error)
  }
)

export default service
